﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class ScrollSelectUI : MonoBehaviour
{
    List<ScrollSelectItem> m_Items = new List<ScrollSelectItem>();

    public ItemListManagerVertitcal listManagerVertical;

    int m_CurIndex;
    int m_ShowingIndex;

    public Transform itemsParent;
    public ScrollSelectItem itemPref;

    string[] values;

    void Start()
    {
        itemPref.gameObject.SetActive(false);
    }
    public void Init(string[] _values)
    {
        values = _values;

        listManagerVertical.maxIndex = values.Length - 1;

        UnityFunctions.ClearList(m_Items);
        for (int i = 0; i < values.Length; i++)
        {
            var newItem = Instantiate(this.itemPref, itemsParent);
            newItem.gameObject.SetActive(true);

            if (i == 0)
            {
                newItem.ShowActive(false);
            }
            else
            {
                newItem.ShowDisctive(false);
            }

            newItem.Init(values[i]);

            m_Items.Add(newItem);
        }
        ResetPos();
    }

    public void ResetPos()
    {
        var pos = listManagerVertical.transform.localPosition;
        pos.y = 0;
        listManagerVertical.transform.localPosition = pos;
    }


    public Action<int> onShowIndex;
    public Action<int> onIndex;
    void Update()
    {
        if (m_ShowingIndex != this.listManagerVertical.showingIndex)
        {
            m_ShowingIndex = this.listManagerVertical.showingIndex;

            for (int i = 0; i < this.m_Items.Count; i++)
            {
                if (i == m_ShowingIndex)
                {
                    m_Items[i].ShowActive();

                    onShowIndex?.Invoke(i);
                }
                else
                {
                    m_Items[i].ShowDisctive();
                }
            }
        }

        if (m_CurIndex != listManagerVertical.curIndex)
        {
            m_CurIndex = listManagerVertical.curIndex;

            onIndex?.Invoke(m_CurIndex);
        }
    }

    

}
